package com.hy.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.hy.admin.dao.ImageDao;
import com.hy.admin.dao.MemberDao;
import com.hy.admin.dao.QuestionDao;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.entity.TeacherEntity;
import com.hy.admin.enums.EnumImageRelatedType;
import com.hy.admin.enums.EnumMemberType;
import com.hy.admin.helper.request.TeacherRequest;
import com.hy.admin.helper.response.PersonalResponse;
import com.hy.admin.service.TeacherService;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private ImageDao imageDao;

    @Override
    public ServicesResult<List<TeacherEntity>> list(TeacherRequest teacherRequest) {
        ServicesResult<List<TeacherEntity>> servicesResult = new ServicesResult<>();
        if(StringUtils.isBlank(teacherRequest.getKeyword())) {
            //设置分页条件，Parameters:pageNum 页码pageSize 每页显示数量count 是否进行count查询
            Integer pageNum = teacherRequest.getPageNum();
            Integer pageSize = teacherRequest.getPageSize();
            if (pageNum == null || pageNum <= 0) {
                //初始页码为1
                pageNum = 1;
            }
            if (pageSize == null || pageSize <= 0) {
                //初始数量为20
                pageSize = 20;
            }
            PageHelper.startPage(pageNum, pageSize);
        }
        List<TeacherEntity> tList = memberDao.selectTeacher(
                EnumImageRelatedType.HEAD_IMAGE.getId(), EnumMemberType.Teacher.getId(), teacherRequest.getKeyword());
        servicesResult.setResult(tList);
        return servicesResult;
    }

    @Override
    public ServicesResult<PersonalResponse> home(Long memberId) {
        ServicesResult<PersonalResponse> servicesResult = new ServicesResult<>();
        if (memberId == null || memberId <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求参数有误");
            return servicesResult;
        }
        //获取用户对象
        MemberEntity memberEntity = memberDao.selectById(memberId);
        if (memberEntity == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("用户不存在");
            return servicesResult;
        }
        //查询问答数量
        int questionNum = questionDao.selectCountByTeacherId(memberId);

        //处理返回结果
        PersonalResponse personalResponse = new PersonalResponse();
        personalResponse.setNickName(memberEntity.getNickName());
        personalResponse.setIndividualitySign(memberEntity.getIndividualitySign());
        personalResponse.setQuestionNum(questionNum);
        //查询用户头像
        List<String> imageList = imageDao.selectByRelatedIdAndType(memberEntity.getId(), EnumImageRelatedType.HEAD_IMAGE.getId());
        if(imageList != null && imageList.size() > 0){
            personalResponse.setImgPath(imageList.get(0));
        }
        servicesResult.setResult(personalResponse);
        return servicesResult;
    }

    @Override
    public ServicesResult<List<TeacherEntity>> recommend(TeacherRequest teacherRequest) {
        ServicesResult<List<TeacherEntity>> servicesResult = new ServicesResult<>();
        if(StringUtils.isBlank(teacherRequest.getKeyword())) {
            //设置分页条件，Parameters:pageNum 页码pageSize 每页显示数量count 是否进行count查询
            Integer pageNum = teacherRequest.getPageNum();
            Integer pageSize = teacherRequest.getPageSize();
            if (pageNum == null || pageNum <= 0) {
                //初始页码为1
                pageNum = 1;
            }
            if (pageSize == null || pageSize <= 0) {
                //初始数量为20
                pageSize = 20;
            }
            PageHelper.startPage(pageNum, pageSize);
        }
        List<TeacherEntity> tList = memberDao.selectTeacherRecommend(
                EnumImageRelatedType.HEAD_IMAGE.getId(), EnumMemberType.Teacher.getId());
        servicesResult.setResult(tList);
        return servicesResult;
    }

    @Override
    public Integer countByType() {
        return memberDao.selectCountByType(EnumMemberType.Teacher.getId());
    }

    @Override
    public ServicesResult<List<MemberEntity>> listByType(TeacherRequest teacherRequest) {
        ServicesResult<List<MemberEntity>> servicesResult = new ServicesResult<>();
        //设置分页条件，Parameters:pageNum 页码pageSize 每页显示数量count 是否进行count查询
        Integer pageNum = teacherRequest.getPageNum();
        Integer pageSize = teacherRequest.getPageSize();
        if (pageNum == null || pageNum <= 0) {
            //初始页码为1
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            //初始数量为20
            pageSize = 20;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MemberEntity> tList = memberDao.selectByType(EnumMemberType.Teacher.getId());
        servicesResult.setResult(tList);
        return servicesResult;
    }

    @Override
    public ServicesResult<Boolean> audit(TeacherRequest teacherRequest) {
        ServicesResult<Boolean> servicesResult = new ServicesResult<Boolean>();
        servicesResult.setResult(false);
        if(teacherRequest.getTeacherId() == null || teacherRequest.getTeacherId() <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("参数有误");
            return servicesResult;
        }
        if(teacherRequest.getAuditStatus() == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("参数有误");
            return servicesResult;
        }
        int count = memberDao.updateAuditStatus(teacherRequest.getTeacherId(), teacherRequest.getAuditStatus());
        if(count <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("数据修改失败");
            return servicesResult;
        }
        else {
        	servicesResult.setResult(true);
        }
        return servicesResult;
    }

    @Override
    public MemberEntity selectById(Long teacherId) {
        return memberDao.selectById(teacherId);
    }
}
